package com.koron.css2.clientCenter.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.clientCenter.bean.UserInfoMerge;
import com.koron.css2.clientCenter.mapper.UserInfoMergeMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoMergeQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 并户列表查询分页
 *
 * @author 朱超
 * @since 2021-12-24 10:42:05
 */
@Service
public class UserInfoMergeList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMergeMapper userInfoMergeMapper = factory.getMapper(UserInfoMergeMapper.class);

            // 1. 根据请求json获取实例query
            UserInfoMergeQuery userInfoMergeQuery = JsonUtils.objectToPojo(req.getData(),UserInfoMergeQuery.class);
            // 2. 分页
            PageHelper.startPage(userInfoMergeQuery.getPage(), userInfoMergeQuery.getPageCount());
            // 3. 查询数据
            List<UserInfoMerge> userInfoMergeList = userInfoMergeMapper.getList(userInfoMergeQuery);
            // 4. 根据当前的状态添加进度
            for (UserInfoMerge userInfoMerge:userInfoMergeList) {
                String status = userInfoMerge.getProcessState();
                if (status.equals(ProcessStateEnum.DRAFT.getState())) {
                    userInfoMerge.setStateName("");
                } else if (status.equals(ProcessStateEnum.PROCESSING.getState())) {
                    userInfoMerge.setStateName("审批");
                } else if (status.equals(ProcessStateEnum.END.getState())) {
                    userInfoMerge.setStateName("结束");
                } else if (status.equals(ProcessStateEnum.CANCEL.getState())) {
                    userInfoMerge.setStateName("废弃");
                }
            }
            // 5. 返回数据
            MessageBean<PageInfo> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"并户列表查询成功", PageInfo.class);
            msg.setData(new PageInfo(userInfoMergeList));
            return msg;
        } catch (Exception e) {
            logger.error("并户列表查询失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"并户列表查询失败",void.class);
        }
    }
}
